<template>
  <erp-form-pur-order-explain
    v-model="tempValue"
    v-bind="$attrs"
    :form="tempForm"
    :extra="extraCom"
    @value-change="handleModalValueChange"
    @update-id-field="handleModalUpdateField"
    @multiple-insert="handleMultipleInsert"
  />
</template>

<script>
import ErpFormPurOrderExplain from "../../form/ErpFormPurOrderExplain";
import {sourceMap} from "./configs";
import {getTableSourceMapRow} from "../../../../utils/erp";
/**
 * 申购单
 * @author maolin
 */
export default {
  name: "ErpTablePurOrderExplain",
  components: {
    ErpFormPurOrderExplain,
  },
  model: {
    prop: "value",
    event: "value-change"
  },
  props: {
    /**
     * 表单对象
     * @public
     */
    form: {
      type: Object,
      default () {
        return {};
      }
    },
    /**
     * 选择的默认值
     * @public
     */
    value: {
      type: [String, Number],
      default: ''
    },
    /**
     * 额外配置
     * @public
     */
    extra: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data () {
    return {
      tempValue: null,
      tempForm: null,
    };
  },
  computed: {
    extraCom () {
      const map = sourceMap;
      let extraData = [];
      for (let i in map) {
        extraData.push({
          field: map[i],
          relatedField: i
        });
      }
      return Object.assign({extraData}, this.extra);
    }
  },
  watch: {
    value: {
      handler(value) {
        this.tempValue = value;
      },
      immediate: true
    },
    form: {
      handler(form) {
        this.tempForm = form;
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 将值传递父组件
     * @param data
     * @public
     */
    handleModalValueChange (data) {
      /**
       * 将值传递父组件
       * @param data
       * @public
       */
      this.$emit('value-change', data);
    },
    /**
     * 将表单更新
     * @param data
     * @public
     */
    handleModalUpdateField (data) {
      for (let i in data) {
        if (data[i] === undefined || data[i] === null) {
          delete data[i];
        }
      }
      if (this.tempForm) {
        let newForm = Object.assign(this.tempForm, data);
        this.$emit('form-change', newForm);
      }
    },
    /**
     * 多条数据选择之后插入到表格
     * @public
     */
    handleMultipleInsert (data) {
      const rows = data.rows;
      // 未选择数据，直接返回
      if (rows.length < 1) {
        return;
      }
      // 处理选中的第一条数据
      const firstRow = rows[0];
      let firstData = {};
      for(let i in sourceMap) {
        firstData = getTableSourceMapRow({row: firstData, sourceMap, insertRow: firstRow, i});
      }
      this.handleModalUpdateField(firstData);
      if (rows.length > 1) {
        // 若选择了多行
        let returnRows = [];
        let insertRows = rows.slice(1);
        for (let i in insertRows) {
          let insertRow = insertRows[i];
          let row = {};
          for (let j in sourceMap) {
            row = getTableSourceMapRow( {row,sourceMap, insertRow, i: j});
          }
          returnRows.push(row);
        }
        this.$emit('rows-insert', {rows: returnRows});
      }
    }
  }
};
</script>

<style scoped>

</style>
